<?php
require("Utils.php");
class RichTextAreaManipulator
{


public static function find_and_replace_span_tag(&$mainString, &$startid, &$endid)
{
	$endid = -1;
	$i = 0;
	$l = strlen($mainString);
	$strtofind1 = "<span style=";
	$j = 0;
	$l2 = strlen($strtofind1);
	$strtofind2 = "</span>";
	$k = 0;
	$l3 = strlen($strtofind2);
	$searchstate = 0;
	$searcjump = false;
	$init = $startid;
	for($i = $init; $i < $l; $i++)
	{
		if($searchstate == 0 || $searchstate == 1)
		{
			if($mainString[$i] == $strtofind1[$j])
			{
				$j++;
			}
			else
			{
				$j = 0;						//oggetto nn corrisponde e continua la ricerca della stringa 1
			}
			if($j == $l2)
			{
				$startid = $i-$l2;
				if($searchstate == 1)		//trovato span interno e quindi si focalizza la ricerca su di esso
				{
					$searcjump = true;		//c'è uno span esterno da analizzare in seguito
					$k = 0;					//si cerca la chiusura di questo
				}
				else
				{
					$searchstate = 1;		//oggetto iniziale trovato e prosegue la ricerca dell'oggetto 2
					$j = 0;
				}
			}
		}
		if($searchstate == 1)
		{
			if($mainString[$i] == $strtofind2[$k])
			{
				$k++;
			}
			else
			{
				$k = 0;						//oggetto nn corrisponde e continua la ricerca della stringa 2
			}
			if($k == $l3)
			{
				$endid = $i;
				break;
			}
		}
	}
	if($endid != -1)
	{
		$endspan = 0;
		$toreplace = "";
		$toreplacend = "";
		$i = $startid;
		while($mainString[$i] != '>')
			$i++;
		$spanstr = substr($mainString,$startid,$endid-$startid);
		if(!(strpos($spanstr,"bold") === FALSE))
		{
			$toreplace .= "<b>";
			$toreplacend .= "</b>";
		}
		if(!(strpos($spanstr,"italic") === FALSE))
		{
			$toreplace .= "<i>";
			$toreplacend .= "</i>";
		}
		if(!(strpos($spanstr,"underline") === FALSE))
		{
			$toreplace .= "<u>";
			$toreplacend .= "</u>";
		}
		$usecolor = strpos($spanstr,"color:");
		if(!($usecolor === FALSE))
		{
			$i = $usecolor+6;//6 = strlen("color:");
			while($spanstr[$i] != ')')
				$i++;
			$toreplace .= "<font color=" . substr($spanstr,$usecolor+6,$i-($usecolor+6)) . " >";
			$toreplacend .= "</font>";
		}
		$mainString = substr($mainString,0,$startid) . $toreplace . substr($mainString,$startid+strlen($spanstr),$endid-$l3) . $toreplacend . substr($mainString,$endid+1,$l-$endid+1);
	}
	else
		$startid = $endid = $l;
	return $searcjump;
}

public static function span_tag_to_simple($textstr)
{
	$newtext = $textstr;
	$newtext = str_replace("<br\\>","<br/>",$newtext);
	$newtext = str_replace("<\\p>","</p>",$newtext);
	$newtext = str_replace("<P ","<p ",$newtext);
	$newtext = str_replace("<A ","<a ",$newtext);
	$newtext = str_replace("<\\a>","</a>",$newtext);
	$newtext = str_replace("<\\div>","</div>",$newtext);
	$newtext = str_replace("<\\span>","</span>",$newtext);
	$newtext = str_replace("<\\strong>","</b>",$newtext);
	$newtext = str_replace("<\\b>","</b>",$newtext);
	$newtext = str_replace("</strong>","</b>",$newtext);
	$newtext = str_replace("<strong>","<b>",$newtext);
	$newtext = str_replace("<\\em>","</u>",$newtext);
	$newtext = str_replace("<\\u>","</u>",$newtext);
	$newtext = str_replace("</em>","</u>",$newtext);
	$newtext = str_replace("<em>","<u>",$newtext);
	$newtext = str_replace("<\\i>","</i>",$newtext);
	$newtext = str_replace("<BR\\>","<br/>",$newtext);
	$newtext = str_replace("<\\P>","</p>",$newtext);
	$newtext = str_replace("<\\A>","</a>",$newtext);
	$newtext = str_replace("<\\DIV>","</div>",$newtext);
	$newtext = str_replace("<\\SPAN>","</span>",$newtext);
	$newtext = str_replace("<\\STRONG>","</b>",$newtext);
	$newtext = str_replace("<\\B>","</b>",$newtext);
	$newtext = str_replace("</STRONG>","</b>",$newtext);
	$newtext = str_replace("<STRONG>","<b>",$newtext);
	$newtext = str_replace("<\\EM>","</u>",$newtext);
	$newtext = str_replace("<\\U>","</u>",$newtext);
	$newtext = str_replace("</EM>","</u>",$newtext);
	$newtext = str_replace("<EM>","<u>",$newtext);
	$newtext = str_replace("<\\I>","</i>",$newtext);
	$newtext = str_replace("\"","'",$newtext);
	$findid = 0;
	$replacement_end = 0;
	$length = strlen($newtext);
	while($findid < $length)
	{
		if(RichTextAreaManipulator::find_and_replace_span_tag($newtext, $findid, $replacement_end) == true)
			$findid = 0;
		else
			$findid = $replacement_end+1;
	}
	return $newtext;
}

public static function html_tag_to_flash_tag($textstr)
{
	$newtext = $textstr;
	/*$newtext = str_ireplace("<br\\>","<br/>",$newtext);
	$newtext = str_ireplace("<\\p>","</p>",$newtext);
	$newtext = str_ireplace("<\\a>","</a>",$newtext);
	$newtext = str_ireplace("<\\div>","</div>",$newtext);
	$newtext = str_ireplace("<\\span>","</span>",$newtext);
	$newtext = str_ireplace("<\\strong>","</b>",$newtext);
	$newtext = str_ireplace("<\\b>","</b>",$newtext);
	$newtext = str_ireplace("</strong>","</b>",$newtext);
	$newtext = str_ireplace("<strong>","<b>",$newtext);
	$newtext = str_ireplace("<\\em>","</u>",$newtext);
	$newtext = str_ireplace("<\\u>","</u>",$newtext);
	$newtext = str_ireplace("</em>","</u>",$newtext);
	$newtext = str_ireplace("<em>","<u>",$newtext);
	$newtext = str_ireplace("<\\i>","</i>",$newtext);*/
	$newtext = str_ireplace("<br\\>","<br/>",$newtext);
	$newtext = str_ireplace("<\\p>","</p>",$newtext);
	$newtext = str_ireplace("<P ","<p ",$newtext);
	$newtext = str_ireplace("<A ","<a ",$newtext);
	$newtext = str_ireplace("<\\a>","</a>",$newtext);
	$newtext = str_ireplace("<\\div>","</div>",$newtext);
	$newtext = str_ireplace("<\\span>","</span>",$newtext);
	$newtext = str_ireplace("<\\strong>","</b>",$newtext);
	$newtext = str_ireplace("<\\b>","</b>",$newtext);
	$newtext = str_ireplace("</strong>","</b>",$newtext);
	$newtext = str_ireplace("<strong>","<b>",$newtext);
	$newtext = str_ireplace("<\\em>","</u>",$newtext);
	$newtext = str_ireplace("<\\u>","</u>",$newtext);
	$newtext = str_ireplace("</em>","</u>",$newtext);
	$newtext = str_ireplace("<em>","<u>",$newtext);
	$newtext = str_ireplace("<\\i>","</i>",$newtext);
	$newtext = str_ireplace("<BR\\>","<br/>",$newtext);
	$newtext = str_ireplace("<\\P>","</p>",$newtext);
	$newtext = str_ireplace("<\\A>","</a>",$newtext);
	$newtext = str_ireplace("<\\DIV>","</div>",$newtext);
	$newtext = str_ireplace("<\\SPAN>","</span>",$newtext);
	$newtext = str_ireplace("<\\STRONG>","</b>",$newtext);
	$newtext = str_ireplace("<\\B>","</b>",$newtext);
	$newtext = str_ireplace("</STRONG>","</b>",$newtext);
	$newtext = str_ireplace("<STRONG>","<b>",$newtext);
	$newtext = str_ireplace("<\\EM>","</u>",$newtext);
	$newtext = str_ireplace("<\\U>","</u>",$newtext);
	$newtext = str_ireplace("</EM>","</u>",$newtext);
	$newtext = str_ireplace("<EM>","<u>",$newtext);
	$newtext = str_ireplace("<\\I>","</i>",$newtext);
	$newtext = Utils::encode_quotes($newtext);
	$newtext = str_replace("\\","",$newtext);
	$newtext = str_replace("\n","",$newtext);
	$newtext = str_replace("\r","",$newtext);
	$newtext = str_replace("\t","",$newtext);
	return $newtext;
}

public static function RichTextAreaToMemBuffer($code)
{
	return RichTextAreaManipulator::html_tag_to_flash_tag($code);
}

public static function MemBufferToRichTextArea($code)
{
	$val = $code;
	$val = Utils::decode_quotes($val);
	$val = str_replace('"','\\"',$val);
	$val = "\"" . $val . "\"";
	return ($val == "\"\"") ? "\" \"" : $val;
}

public static function MemBufferToOutput($code)
{
	return Utils::decode_quotes($code);
}


}

?>